package com.example.a10928.weifriend.ui.home.menu

import android.annotation.SuppressLint
import android.content.Intent
import android.databinding.ViewDataBinding
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.support.constraint.ConstraintLayout
import android.view.View
import android.widget.ImageView
import com.example.a10928.weifriend.App.app
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.databinding.ActivityQrBinding
import com.example.a10928.weifriend.http.api.RowsWrapper
import com.example.a10928.weifriend.ui.DatabindingBaseActivity
import com.example.a10928.weifriend.util.L
import com.example.a10928.weifriend.util.QRCodeUtil
import com.example.a10928.weifriend.util.RxUtil
import com.hd.viewcapture.CaptureManager
import com.hd.viewcapture.ViewCapture
import io.reactivex.ObservableEmitter
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import me.leefeng.promptlibrary.PromptDialog
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException

class QRKActivity : DatabindingBaseActivity(),CaptureManager.OnSaveResultListener {
    override fun onSaveResult(isSaved: Boolean, path: String?, uri: Uri?) {

        L.t("保存图片成功")
    }

    lateinit var mBinding: ActivityQrBinding
    var bitmap: Bitmap? = null

    var parentLayout: ConstraintLayout?=null
    override fun bindingInitData() {
    }

    override fun bindingInitView(viewDataBinding: ViewDataBinding?) {
        setTitleText(ThemeEnum.Back_Default, "推广二维码")
        mBinding = viewDataBinding as ActivityQrBinding
        mBinding.view = this
        getInvitation()

        parentLayout=findViewById(R.id.parent_layout) as ConstraintLayout


        var qr_invitation_iv=findViewById(R.id.qr_invitation_iv) as ImageView
        qr_invitation_iv.setOnLongClickListener {
            ViewCapture.with(parentLayout as View).asJPG().setOnSaveResultListener(this@QRKActivity).save()

            true
        }
    }

    override fun bindingGetContentView(): Int {
        return R.layout.activity_qr
    }


    public fun onViewClick(v: View) {
        when (v.id) {
            R.id.qr_save_photo -> {
/*                if (bitmap != null) {
                    val dialog = PromptDialog(this)
                    dialog.showLoading("正在保存图片")
                    io.reactivex.Observable.create(ObservableOnSubscribe<Bitmap> { e ->
                        saveBitmap(bitmap!!, System.currentTimeMillis().toString() + "")
                        e.onNext(bitmap!!)
                    }).subscribeOn(Schedulers.newThread())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe {
                                dialog.dismiss()
                                L.t("保存完毕")
                            }
                }*/

                ViewCapture.with(parentLayout as View).asJPG().setOnSaveResultListener(this@QRKActivity).save();
            }
            else -> {
            }
        }
    }



    @SuppressLint("CheckResult")
    private fun getInvitation() {
        app().bpService.getExtendQr(app().user.userId)
                .compose(RxUtil.io2mainLoad(this))
                .subscribe(
                        { r ->
                            bitmap = QRCodeUtil.createQRCodeBitmap(r.data.qr_code, 480, 480)
                            mBinding.qrInvitationIv.setImageBitmap(bitmap)
                            mBinding.qrInvitationInfo.text = "邀请码: "+r.data.invitation_code
                        }, {
                    RxUtil::errorException
                }
                )
    }

}